<%# The license inside this block applies to this file.
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
# frozen_string_literal: false

<%= lines(autogen_notice(:ruby, pwd)) -%>
require 'gcp_backend'
class <%= object.__product.name.camelize(:upper) -%><%= object.name -%>s < GcpResourceBase
<%
name = resource_name(object, product)
-%>
  name '<%= name.pluralize -%>'
  desc '<%= object.name -%> plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create
<% object.all_user_properties.reject(&:exclude_plural).each do |prop| -%>

  <%= "filter_table_config.add(:#{(prop.override_name || prop.out_name).pluralize}, field: :#{prop.override_name || prop.out_name})" -%>
<% end # object.all_user_properties.each do -%>
<% object.plural_custom_attr_readers.each do |custom_attr_reader| -%>

  <%= "filter_table_config.add(:#{custom_attr_reader.pluralize}, field: :#{custom_attr_reader})" -%>
<% end -%>


  filter_table_config.connect(self, :table)

<% 
link_query_items = object&.nested_query&.keys&.first || object.collection_url_key
-%>
  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
<%= indent("@table = fetch_wrapped_resource('#{link_query_items}')", 4) %>
  end

  def fetch_wrapped_resource(wrap_path)
    # fetch_resource returns an array of responses (to handle pagination)
    result = @connection.fetch_all(product_url, resource_base_url, @params, '<%= object.plural_fetch_verb.capitalize -%>')
    return if result.nil?

    # Conversion of string -> object hash to symbol -> object hash that InSpec needs
    converted = []
    result.each do |response|
      next if response.nil? || !response.key?(wrap_path)
      response[wrap_path].each do |hash|
        hash_with_symbols = {}
        hash.each_key do |key|
          name, value = transform(key, hash)
          hash_with_symbols[name] = value
        end
<% if object.plural_custom_logic -%>
<%= lines(indent(compile(pwd + '/' + object.plural_custom_logic), 8)) -%>
<% end -%>
        converted.push(hash_with_symbols)
      end
    end

    converted
  end

  def transform(key, value)
    return transformers[key].call(value) if transformers.key?(key)

    [key.to_sym, value]
  end

  def transformers
    {
<% object.all_user_properties.reject(&:exclude_plural).each do |prop| -%>
      '<%= prop.api_name -%>' => ->(obj) { <%= "return :#{ prop.override_name || prop.out_name }, #{parse_code(prop, 'obj')}" -%> },
<% end # object.all_user_properties.each -%>
    }
  end
<% if time_prop?(object.all_user_properties) -%>

  # Handles parsing RFC3339 time string
  def parse_time_string(time_string)
    time_string ? Time.parse(time_string) : nil
  end
<% end # has_time_prop -%>

  private

<%= compile(pwd + '/templates/inspec/product_url.erb') -%>

  def resource_base_url
    '<%= object.base_url %>'
  end
end
